package com.hsteach.web.controller.cookmenu;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Maps;
import com.hsteach.common.annotation.Anonymous;
import com.hsteach.common.config.AppConfig;
import com.hsteach.common.core.controller.BaseController;
import com.hsteach.common.core.domain.AjaxResult;
import com.hsteach.common.core.page.TableDataInfo;
import com.hsteach.common.exception.ServiceException;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.common.utils.poi.ExcelUtil;
import com.hsteach.service.cookmenu.CookmenuWeekService;
import com.hsteach.web.controller.cookmenu.dto.*;
import com.hsteach.web.controller.cookmenu.vo.ListMenuVo;
import com.hsteach.web.controller.cookmenu.vo.SemWeek;
import com.hsteach.web.controller.meeting.dto.GetWeekDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.hsteach.dao.domain.cookmenu.CookmenuWeek;
import org.springframework.web.multipart.MultipartFile;

/**
 * 食堂每周菜谱明细Controller
 * 
 * @author fanxquer
 * @date 2023-11-05
 */
@Api(value = "食堂每周菜谱明细", tags = "食堂每周菜谱明细")
@RestController
@RequestMapping("/cookmenu")
public class CookmenuWeekController extends BaseController
{
    @Autowired
    private CookmenuWeekService cookmenuWeekService;

    @Autowired
    private AppConfig appConfig;

    /**
     * 查询食堂每周菜谱明细列表
     */
    @ApiOperation("查询一周的菜谱表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ListDishDto dto)
    {
        List<ListMenuVo> list = cookmenuWeekService.selectCookmenuWeekList(dto);
        return getDataTable(list);
    }

    @ApiOperation("根据起止时间获取周次")
    @PostMapping("/getWeek")
    public TableDataInfo getWeek(@RequestBody GetWeekDto dto)
    {
        if (dto.getBeginDay().getTime() > dto.getEndDay().getTime()) {
            throw new ServiceException("开始时间不能大于结束时间");
        }
        List<SemWeek> list = cookmenuWeekService.getWeekList(dto.getBeginDay(), dto.getEndDay());
        return getDataTable(list);
    }

    @Anonymous
    @ApiOperation("下载食谱导入模版")
    @GetMapping("/download/template")
    public AjaxResult export(HttpServletResponse response)
    {
        String templateUrl = appConfig.getConfig().get(SecurityUtils.getAuthInfo().getAppId()).getTemplateUrl();
        AjaxResult success = AjaxResult.success();
        Map<String, String> data = Maps.newHashMap();
        data.put("url", templateUrl);
        success.put("data", templateUrl);
//        success.put("data", data);
        return success;
    }

    @ApiOperation("导入食谱")
    @PostMapping("/import")
    public AjaxResult uploadFile(ImportDishDto dto, MultipartFile file) throws Exception {
        return AjaxResult.success(cookmenuWeekService.importFile(dto, file.getInputStream()));
    }


    /**
     * 新增食堂每周菜谱明细
     */
    @ApiOperation("新增食堂菜品")
    @PostMapping
    public AjaxResult addDish(@RequestBody AddDishDto dto)
    {
        return toAjax(cookmenuWeekService.addDish(dto));
    }

    /**
     * 修改食堂每周菜谱明细
     */
    @ApiOperation("修改食堂菜品")
    @PutMapping
    public AjaxResult edit(@RequestBody UpdateDishDto dto)
    {
        return toAjax(cookmenuWeekService.updateDish(dto));
    }

    /**
     * 复制到其他周
     */
    @ApiOperation("复制到其他周")
    @PostMapping("/copy")
    public AjaxResult copy(@RequestBody @Validated CopyDishDto dto)
    {
        return toAjax(cookmenuWeekService.copyDish(dto));
    }

    /**
     * 删除食堂每周菜谱明细
     */
    @ApiOperation("删除食堂菜品")
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(cookmenuWeekService.deleteCookmenuWeekById(id));
    }

    /**
     * 删除食堂每周菜谱明细
     */
    @ApiOperation("点赞食堂菜品")
    @PostMapping("/like")
    public AjaxResult like(@RequestBody LikeDto dto)
    {
        return toAjax(cookmenuWeekService.like(dto));
    }

    /**
     * 删除食堂每周菜谱明细
     */
    @ApiOperation("撤销点赞食堂菜品")
    @PostMapping("/notlike")
    public AjaxResult notlike(@RequestBody LikeDto dto)
    {
        return toAjax(cookmenuWeekService.notlike(dto));
    }
}
